<?php

namespace other;



use think\Db;

/**
 * 短信验证码类
 */
class Sms
{

    /**
     * 验证码有效时长
     * @var int
     */
    protected static $expire = 600;

    /**
     * 最大允许检测的次数
     * @var int
     */
    protected static $maxCheckNums = 10;

    /**
     * 获取最后一次手机发送的数据
     *
     * @param   int       $mobile   手机号
     * @param   string    $event    事件
     * @return  Sms
     */
    public static function get($mobile, $event = 'default')
    {
        $sms = \db('sms')->where(['mobile' => $mobile, 'event' => $event])
            ->order('id', 'DESC')
            ->find();
        return $sms ? $sms : NULL;
    }

    /**
     * 发送验证码
     *
     * @param   int       $mobile       手机号
     * @param   int       $code         验证码,为空时将自动生成4位数字
     * @param   string    $event        事件、
     * @param   string    $sign_name    短信签名
     * @return  boolean
     */
    public static function send($mobile,$perfix = '', $code = NULL, $event = 'default', $sign_name = ""){

        $code = is_null($code) ? mt_rand(100000, 999999) : $code;
        $time = time();
        $ip = request()->ip();
        $sms = \db('sms')->insertGetId(
            [   'event' => $event,
                'mobile' => $mobile,
                'perfix'=>$perfix,
                'code' => $code,
                'ip' => $ip,
                'createtime' => $time
            ]);
        $sign_name = !empty($sign_name) ? $sign_name : config('database')['host_sms_name'];
        $content = '【' . $sign_name . '】您的短信验证码为：' . $code. '，5分钟内有效，若非本人操作请忽略。';

//        writelog("发送验证码:手机号{$mobile}，内容{$content},验证码：{$code}", 'sendsms');
//        $sendRes = SmsWYSend::sendCode($mobile, $content);
//        if (empty($sendRes)) {
//            \db('sms')->where('id', $sms)->delete();
//            writelog("发送验证码失败2", 'sendsms');
//            return false;
//        }
//        $res = json_decode($sendRes, true);
//        if($res['result'] != 0){
//            \db('sms')->where('id', $sms)->delete();
//            writelog("发送验证码失败3:" . $res['desc'], 'sendsms');
//            return false;
//        }
        $sendRes = SmsMuSend::send($mobile, $content);
        writelog("发送验证码:手机号{$mobile}，内容{$content},验证码：{$code}，返回：".strval($sendRes), 'sendsms');

        $xmlObject = simplexml_load_string($sendRes);
        $json = json_encode($xmlObject);
        $res = json_decode($json, true);

        return $res;
    }

    /**
     * 校验验证码
     *
     * @param   int       $mobile     手机号
     * @param   int       $code       验证码
     * @param   string    $event      事件
     * @return  boolean
     */
    public static function check($mobile, $perfix = '86',$code, $event = 'default')
    {
        $time = time() - self::$expire;

        $sms = \db('sms')->where(['mobile' => $mobile, 'event' => $event,'perfix'=>$perfix])
            ->order('id', 'DESC')
            ->find();
        if (empty($sms)) {
            return false;
        }

        if ($sms['createtime'] > $time) {
            $correct = $code == $sms['code'];
            if (!$correct) {
                return false;
            } else {
                //验证成功则返回sms信息
                return $sms;
            }
        } else {
            // 过期则清空该手机验证码
            self::flush($mobile, $event);
            return false;
        }

    }

    /**
     * 清空指定手机号验证码
     *
     * @param   int       $mobile     手机号
     * @param   string    $event      事件
     * @return  boolean
     */
    public static function flush($mobile, $event = 'default')
    {
        \db('sms')->where(['mobile' => $mobile, 'event' => $event])->delete();
        return TRUE;
    }

}
